﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LOGISTCBUS.Entities;
using LOGISTCBUS.DAO;

namespace LOGISTCBUS.WebUI.Controllers
{
    public class GMapController : Controller
    {
        public ActionResult Index(int? id)
        {
            var user = (User)this.Session["user"];
            if (user == null)
                return RedirectToAction(@"LogOut", @"Account");

            if (id.HasValue)
            {
                var trip = new TripDAO().selectById(Convert.ToInt32(id));
                this.ViewData[@"trip"] = trip == null ? new Trip() : trip;
                this.ViewData[@"idTrip"] = trip == null ? new Nullable<Int32>() : trip.Id;
            }

            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult Load()
        {
            var id = String.IsNullOrEmpty(Request.Params[@"idTrip"]) ? 0 : Convert.ToInt32(Request.Params[@"idTrip"]);
            var t = new Trip();

            if (id > 0)
                t = new TripDAO().selectById(id);

            var obj = new
            {
                Origin = t != null && t.Id > 0 ? t.Origin : string.Empty,
                Destination = t != null && t.Id > 0 ? t.Destination : string.Empty
            };

            return Json(obj);
        }
    }
}
